/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.backingbean;

import com.aal.entitybean.Products;
import com.aal.sessionbean.product.ProductsFacadeLocal;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.ejb.EJB;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Professional
 */
@ManagedBean
@RequestScoped
public class ProductManagedBean {

    @EJB
    private ProductsFacadeLocal productsFacade;
    private Products pro;

    public Products getPro() {
        return pro;
    }

    public void setPro(Products pro) {
        this.pro = pro;
    }

    public ProductsFacadeLocal getProductsFacade() {
        return productsFacade;
    }

    public void setProductsFacade(ProductsFacadeLocal productsFacade) {
        this.productsFacade = productsFacade;
    }

    /** Creates a new instance of ProductManagedBean */
    public ProductManagedBean() {
    }
    private List<Products> listPro;

    public void showProduct() {
	try{
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();
        Map map = externalContext.getRequestParameterMap();
        String cateId = (String) map.get("cateID").toString();
        System.out.println("showProduct cateID : " + cateId);
        HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true);
        session.setAttribute("cateID", cateId);
		}catch(Exception ex)
		{
		 try {
                HttpServletResponse Response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
                Response.sendRedirect("demo.xhtml");
            } catch (IOException ex1) {
                Logger.getLogger(ShoppingCartManagedBean.class.getName()).log(Level.SEVERE, null, ex1);
            }
		}
		
    }

    public List<Products> getListPro() {
        try{
		FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true);
        String cateId = (String) session.getAttribute("cateID");
        listPro = productsFacade.findByCategoryId(cateId);
        
		}catch (Exception ex) {
            try {
                HttpServletResponse Response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
                Response.sendRedirect("demo.xhtml");
            } catch (IOException ex1) {
                Logger.getLogger(ShoppingCartManagedBean.class.getName()).log(Level.SEVERE, null, ex1);
            }

        }
		return listPro;
    }

    public void setListPro(List<Products> listPro) {
        this.listPro = listPro;
    }
    private List<Products> listallPro;

    public List<Products> getListallPro() {
       try{
	    listallPro = productsFacade.findAll();
	   }catch (Exception ex) {
            try {
                HttpServletResponse Response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
                Response.sendRedirect("demo.xhtml");
            } catch (IOException ex1) {
                Logger.getLogger(ShoppingCartManagedBean.class.getName()).log(Level.SEVERE, null, ex1);
            }

        }
        return listallPro;
    }

    public Products FindProductbyID() {
       try{
	    FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();
        Map map = externalContext.getRequestParameterMap();
        String prodId = (String) map.get("proID").toString();
        pro = productsFacade.find(prodId);
        checkQuantityPro(pro);
	   }catch (Exception ex) {
            try {
                HttpServletResponse Response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
                Response.sendRedirect("demo.xhtml");
            } catch (IOException ex1) {
                Logger.getLogger(ShoppingCartManagedBean.class.getName()).log(Level.SEVERE, null, ex1);
            }

        }
        return pro;

    }

    public void setListallPro(List<Products> listallPro) {
        this.listallPro = listallPro;
    }

    public void addToShoppingCart() {
    }

    public boolean getExistProduct(String obj) {
        System.out.println("Ok 123 : " + obj);
        return false;
    }
    private boolean exist;

    public boolean isExist() {
        return exist;
    }

    public void setExist(boolean exist) {
        this.exist = exist;
    }

    private boolean isRemain;
    public void checkQuantityPro(Products products) {

        if(products.getQuantity()==0){
            isRemain=false;
        }else{
            isRemain=true;
        }

    }

    public boolean isIsRemain() {
        return isRemain;
    }

    public void setIsRemain(boolean isRemain) {
        this.isRemain = isRemain;
    }


}
